package org.sgroup.imenu.controladapter;

import java.util.List;
import java.util.Random;
import org.sgroup.imenu.DetailItemView;
import org.sgroup.imenu.IMenuActivity;
import org.sgroup.imenu.R;
import org.sgroup.imenu.bo.Item;
import org.sgroup.imenu.util.CData;
import org.sgroup.imenu.util.LoadBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class ItemListAdapter extends ArrayAdapter<Item> {

	Context context;
	int resourceid;
	List<Item> objects;

	public ItemListAdapter(Context context, int textViewResourceId,
			List<Item> objects) {
		super(context, textViewResourceId, objects);

		this.context = context;
		this.resourceid = textViewResourceId;
		this.objects = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater flater = LayoutInflater.from(context);
			convertView = flater.inflate(resourceid, null);
		}
		
		ImageButton btnThumbnail = (ImageButton) convertView.findViewById(R.id.item_thumbnail);
		final ImageButton btnFind = (ImageButton) convertView.findViewById(R.id.item_find);
		TextView tvItemName = (TextView) convertView.findViewById(R.id.item_name);
		TextView tvItemDesc = (TextView) convertView.findViewById(R.id.item_desc);
		TextView tvItemPrice = (TextView) convertView.findViewById(R.id.item_price);
		TextView tvItemTime = (TextView) convertView.findViewById(R.id.item_time);
		Button btnAddCart = (Button) convertView.findViewById(R.id.btnAddCart);					

		// get current item
		final Item currentItem = objects.get(position);

		tvItemName.setText(currentItem.Name);
		tvItemDesc.setText(currentItem.Description);
		tvItemPrice.setText(CData.FormatPrice(currentItem.Price));
//		Random randTime = new Random();		
//		tvItemTime.setText(String.valueOf(15 + randTime.nextInt(30)));
		tvItemTime.setText(String.valueOf(currentItem.Time));
		if (!currentItem.ImageFile.isEmpty()) {
			try {								
				Bitmap bm= LoadBitmap.GetBitmap(this.context,currentItem.ImageFile);
				btnThumbnail.setImageBitmap(bm);
			} catch (Exception e) {
				Log.v("ItemListAdapter", e.getMessage());
			}
		}		
		
		// click button btnAddCart
		btnAddCart.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				((IMenuActivity)context).AddOrder(currentItem);				
			}
		});
		
		// click button btnThumbnail
		btnThumbnail.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {								
				btnFind.performClick();
			}
		});
		
		// click button btnFind
		btnFind.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {				
				DetailItemView newView = new DetailItemView(context, currentItem);
				((IMenuActivity)context).ChangeView(newView, false);
			}
		});
		
		return convertView;
	}
}
